#pragma warning( push )           // memorize the warning status
#pragma warning (disable:4786)   // disable  identifier was truncated to '255' characters in the debug information

#include "tf_Element.h"


TfElement::TfElement()
{
	m_ver = 1;
	m_id = 0;
	m_partId = 0;
	m_isValid = true;
	m_type = 1;
	m_surfId = 0;
	memset(m_nodeIds,0,sizeof(int)*8);
}


TfElement::~TfElement()
{

}

int TfElement::GetType()
{
	return m_type;
}

int TfElement::GetNodes(int nodeIds[8])
{
	int i;

	for(i = 0; i < m_num; i++)
		nodeIds[i] = m_nodeIds[i];
	return m_num;


	return 0;
}

void TfElement::GetNodes(IntVector& nodeIds)
{
	int i;

	for(i = 0; i < m_num; i++)
		nodeIds.push_back(m_nodeIds[i]);
	
}


int  TfElement::GetPartId()
{
	return m_partId;
}

int TfElement::GetSurfId()
{
	return m_surfId;
}

TfElement& TfElement::operator =(TfElement& rhs)
{
	m_ver = rhs.m_ver;
	m_id  = rhs.m_id;
	m_partId = rhs.m_partId;
	m_isValid = rhs.m_isValid;
	m_type = rhs.m_type;
	m_num = rhs.m_num;
	memcpy(m_nodeIds,rhs.m_nodeIds,sizeof(int)*8);

	return *this;
}

/************************************************************************/
/*                                                                      */
/************************************************************************/
TfElementManager::TfElementManager()
{
	AdditionSpace();
	m_ver = 1;
	
}

TfElementManager::~TfElementManager()
{
	int i;
	for(i = 0; i < m_data.size(); i++)
	{
		delete m_data[i];
		m_data[i] = NULL;
	}
	m_data.clear();

}


int TfElementManager::CreateElement(int nodeIds[],int num,int partId)
{
	int i,idx,id;
	TfElement tmp;
	
	if(num <= 4 && num > 0)
	{
		for(i = 0; i < num; i++)
			tmp.m_nodeIds[i] = nodeIds[i];
		id = tmp.m_id = GetMaxId()+1;
		tmp.m_partId = partId;
		tmp.m_type = 1;
		tmp.m_num = num;
	}
	else
		return 0;
	
	
	idx = End();
	AddToDataBase(idx+1,&tmp);
		
	return id;
}

int TfElementManager::CreateSolidElenent(int nodeIds[],int num, int partId)
{
	int i,idx,id;
	TfElement tmp;
	
	if(num >= 4 && num <= 0)
	{
		for(i = 0; i < num; i++)
			tmp.m_nodeIds[i] = nodeIds[i];
		id = tmp.m_id = GetMaxId()+1;
		tmp.m_partId = partId;
		tmp.m_type = 2;
		tmp.m_num = num;
	}
	else
		return 0;
	
	
	idx = End();
	AddToDataBase(idx+1,&tmp);
	return id;
}


void TfElementManager::DeleteElement(int id)
{
	int idx;

	idx = (*this)[id];
	if(idx > -1)
	{
		m_data[idx]->Delete();
	}
}

void TfElementManager::DeleteElements(IntVector& ids)
{
	for(int i = 0; i < ids.size(); i++)
		DeleteElement(ids[i]);
}

int TfElementManager::GetElementNodes(int id,int nodeIds[8])
{
	int idx;

	idx = (*this)[id];
	if(idx > -1)
	{
		TfElement* pEm = (TfElement*)(m_data[idx]);
		return pEm->GetNodes(nodeIds);
		
	}
	return 0;
}

void TfElementManager::GetElementNodes(int id,IntVector& nodeIds)
{
	int idx;
	
	idx = (*this)[id];
	if(idx > -1)
	{
		TfElement* pEm = (TfElement*)(m_data[idx]);
		pEm->GetNodes(nodeIds);
	}
}

int TfElementManager::GetElementPartId(int id)
{
	int idx;
	idx = (*this)[id];
	if(idx > -1)
	{
		TfElement* pEm = (TfElement*)(m_data[idx]);
		return pEm->GetPartId();
	}
	return -1;
}

void TfElementManager::GetPartElements(int pid,IntVector& elems)
{
	int i,end;
	
	end = End();/*may error*/
	if (end <= -1)
	{
		return;
	}
	TfElement* pEm;
	for(i = Begin(); i <= end; i = Next())
	{
		pEm = (TfElement*)(m_data[i]);
		if(pEm->GetPartId() == pid)
			elems.push_back(pEm->GetId());
	}

}

int TfElementManager::GetPartElements(int pid,int* elems )
{
	int i,cnt,end;
	
	if(elems == NULL)
		return -1;
	end = End();/*may error*/
	if (end <= -1)
	{
		return -1;
	}
	TfElement* pEm;
	cnt = 0;
	for(i = Begin(); i <= end; i = Next())
	{
		pEm = (TfElement*)(m_data[i]);
		if(pEm->GetPartId() == pid)
		{
			elems[cnt] = pEm->GetId();
			cnt++;
		}
	}
	return cnt;
}


void TfElementManager::AddToDataBase(int idx, TfGeomObject* obj)
{
	if(idx >-1)
	{
		TfElement* pEm = (TfElement*)(m_data[idx]);
		TfElement* pObj = (TfElement*)obj;
		*pEm = *pObj;

		AddToMap(obj->GetId(),idx);
	}
}

void TfElementManager::AdditionSpace()
{
	int i;
	TfElement* pObj;
	for(i = 0; i < 10000; i++)
	{
		pObj = new TfElement;
		m_data.push_back(pObj);
	}
}

TfElementManager* TfElementManager::Instance()
{
	static TfElementManager emManager;
	return &emManager;
}

#pragma warning( pop )           // memorize the warning status